<?php

/**
 * BaseService.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysmanger\Service;

use Base\Lib\BaseService as Service;

class BaseService extends Service {

    public function __construct()
    {

        parent::__construct();
    }
    
    // 生成密码
    public function createPwd(&$pwd,&$salt)
    {
        $salt = random(4);
        $pwd = authcode($pwd, $salt, 'ENCODE');
        return true;
    }
    
    // 解密
    public function decodePwd(&$encode_pwd, $salt)
    {
        $encode_pwd = authcode($encode_pwd, $salt, 'DECODE');
        
        return true;
    }
    
    /**
     * @desc 验证密码
     * @param string $in_pwd 输入的密码
     * @param string $find_pwd 查询的密码
     * @param string $salt 密码加密key
     * @return bool
     * */
    public function verifyPwd($in_pwd, $find_pwd, $salt)
    {
        // 开始验证
        $decode_pwd = authcode($find_pwd, $salt, 'DECODE');
        if($decode_pwd == $in_pwd)
        {
            return true;
        }
        
        return false;
    }
    
    // 验证新密码
    public function verifyNewPwd($new_pwd, $check_new_pwd='')
    {
        // 验证新密码
        if($check_new_pwd)
        {
            if($new_pwd != $check_new_pwd)
            {
                throw new \Exception('两次密码不一致');
            }
        }
        
        
        $rs = \Base\Lib\Tools\Validator::is_len_in_range($new_pwd, 5, 11, 'UTF-8');
        if(!$rs)
        {
            throw new \Exception('新密码长度不合法');
        }
        
        $rs = \Base\Lib\Tools\Validator::is_alphanum($new_pwd);
        if(preg_match ('/^[A-Za-z]+$/i', $new_pwd) || preg_match ('/^[\d]+$/i', $new_pwd))
        {
            throw new \Exception('新密码由数字字母组成');
        }
        
        return true;
    }

}